# encoding: utf-8

import requests
import json
import datetime
import os
import xlrd
from urllib.parse import quote
from widget.info_box import *
from widget.utils.my_threading_part import MyThreadFunc
# from widget.daoru_page.import_init_page.sqlserver_to_essystem_table__spical_source_init_window import SpicalSourceInitWindow
from widget.db_init.sqlUtils.sqlserverUtils import SqlServerOp
from widget.utils.utils import MyUtils
import time
from widget.daoru_page.import_init_page.import_func.base_func import BaseImportFunc, ProgressSignal, ImportProgressBox
from widget.daoru_page.source_init_page.noteexpress_source_init import NoteexpressInitFunc


# class ProgressSignal(QObject):
#     '''
#     进度条自定义信号
#     '''
#     # 进度条改变信号
#     data_changed = pyqtSignal(int, int, int, name="dataChanged")
#
#     # 导入完成信号
#     data_finished = pyqtSignal(bool, name="dataFinished")


class NoteExpress2Es(BaseImportFunc):
    '''
    sqlserver导入es的方法
    初始化的时候传入 父控件，来源信息字典，目标信息字典
    '''
    def __init__(self, source_dict, target_dict):
        super(NoteExpress2Es, self).__init__(source_dict, target_dict)

    def get_all_data(self, noteexpress_path, operate_dict, related_field_dict=None):
        '''
        读取excl中的数据
        :return:
        '''
        all_data = []

        neit = NoteexpressInitFunc(noteexpress_path)
        ne_data = neit.readNoteExpress()
        ne_source_dict = neit.noteexpress2qApp(ne_data)

        for content in ne_source_dict['com']['field_content_list']:
            data = []
            for i, fields_name in enumerate(self.source_dict['com']['field_name_list']):
                if fields_name in [operate['source_code'] for operate in operate_dict['operate_list']]:
                    data.append(content[i])
            all_data.append(data)

        return all_data

    # # 导入资料方法 start -------------------------------------------------------------------------
    # def start_import_ziliao(self, all_data, RUID, operate_dict, table_parent_widget):
    #     '''
    #     开始导入(资料部分)
    #     :return:
    #     '''
    #     print('加载导入数据...')
    #
    #     # all_data = self.get_all_data(qApp.cx_src['uni']['file_path'])
    #
    #     # print(len(all_data))
    #     # print('all_data: {0}'.format(all_data))
    #     # print(1/0)
    #
    #     # 消息盒子start -------------------------------------------
    #     table_parent_widget.progress_box = ImportProgressBox(my_parent=table_parent_widget, sum_num=len(all_data))
    #
    #     # self.table1.parent().progress_box.setWindowFlag(Qt.WindowStaysOnTopHint)
    #     # def stop_run():
    #     #     self.table1.parent().run_flag = False
    #     # 关闭按钮点击，终止线程
    #     # self.table1.parent().progress_box.close_btn.clicked.connect(stop_run)
    #     # 取消按钮点击，关闭窗口
    #     table_parent_widget.progress_box.show()
    #
    #     # 初始化进度条信号实例
    #     self.sig = ProgressSignal()
    #     # 绑定更新进度槽
    #     self.sig.dataChanged.connect(table_parent_widget.progress_box.update_progress)
    #     # 绑定导入结束槽
    #     self.sig.dataFinished.connect(table_parent_widget.progress_box.import_finished)
    #
    #     # 消息盒子end --------------------------------------------
    #
    #     # 展示进度
    #     def start_run():
    #         data_dict_list = []
    #         for i, field in enumerate(all_data):
    #             field = list(field)  # 元祖转字典
    #             data_dict = {}
    #
    #             # 插入一对一元数据 start -----------------------------------------------
    #             for k, item in enumerate(operate_dict['operate_list']):
    #                 # if k != 10100001:
    #                 if not field[k]:
    #                     # print('field[k]: {0}-{1}'.format(k, field[k]))
    #                     if item['target_code'] != '10100001':
    #                         continue
    #                     else:
    #                         field[k] = ' '
    #                         # print(1/0)
    #                 data_dict[item['target_code']] = field[k]
    #
    #             data_dict_list.append(data_dict)
    #             print('data_dict: {0}'.format(data_dict))
    #
    #             if (i + 1) % self.speed == 0 or (i + 1) == len(all_data):
    #                 '''
    #                 {
    #                     "RUID": "任务唯一标识",
    #                     "data": [
    #                       {"RUID":"","10100001":"test","1590001":["1","2"]}
    #                     ]
    #                 }
    #                 '''
    #                 data_dict_encoding = quote(json.dumps(data_dict_list, ensure_ascii=False))
    #                 # print('data_dict_encoding: {0}'.format(data_dict_encoding))
    #
    #                 # 导入数据格式 start --------------------------------
    #                 import_dict = {
    #                     "RUID": RUID,
    #                     "data": data_dict_encoding,
    #                     "appendix": quote(json.dumps([], ensure_ascii=False))
    #                     # "data": [data_dict]
    #                 }
    #                 # 导入数据格式 end --------------------------------
    #
    #                 # 进度(第一次/重新传递数据传递start,最后一批数据传递finish，中间不传递)
    #                 if (i + 1) == self.speed:
    #                     import_dict['schedule'] = 'start'
    #                 if (i + 1) == len(all_data):
    #                     import_dict['schedule'] = 'finish'
    #
    #                 # url = qApp.cx_es_db_ip + '/import/importdata'
    #                 # url = qApp.cx_es_db_ip + '/import/importdataorappendix'
    #                 url = qApp.cx_es_db_ip + '/dataprocessingapi/dataimport/importdataorappendix.action'
    #                 print('请求导入。。。')
    #                 print('当前任务RUID:{0}'.format(RUID))
    #                 res = requests.post(url, data=import_dict, verify=False)
    #                 # print('请求返回。。。')
    #                 print('后台返回值：{0}'.format(res.status_code))
    #                 print('当前进度： {0}'.format(i))
    #
    #                 if res.status_code == 500:
    #                     print('报错信息{0}：'.format(res.text))
    #                     return
    #                     # return_dict = {'state': 500}
    #                     # return return_dict
    #                 field_dict = json.loads(res.text) if res.text else None
    #                 # print(field_dict)
    #                 # 请求结果状态
    #                 state = field_dict['cxajaxrc']
    #                 print('state: {0}'.format(state))
    #                 if state == '-1':
    #                     self.add_err_log('资料进度：{0}；请求成功，返回-1，报错信息：{1}'.format(i, field_dict[
    #                         'cxajaxerrmsg'] if 'cxajaxerrmsg' in field_dict else ''))
    #                     print('请求成功，返回错误：{0}'.format(field_dict['cxajaxerrmsg']))
    #                 if state == '0':
    #                     # returnvalue = field_dict['returnvalue']
    #                     # if 'cxajaxloglist' in returnvalue:
    #                     #     self.add_err_log('资料进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(
    #                     #         returnvalue['cxajaxloglist'] if 'cxajaxloglist' in returnvalue else '')))
    #
    #                     # 插入日志 start -----------------------------------------------------
    #                     try:
    #                         returnvalue = field_dict['returnvalue']
    #                         if 'cxajaxloglist' in returnvalue:
    #                             log_str = ''
    #                             for u in range(len(returnvalue['cxajaxloglist'])):
    #                                 cur_num = (i + 1) - 25 + (u + 1)
    #                                 log_str += '资料进度：{0}-{1}；请求成功，返回0，导入信息：{2}\n'.format(
    #                                     cur_num,
    #                                     data_dict_list[u]['10100001'],
    #                                     str(returnvalue['cxajaxloglist'][u] if returnvalue['cxajaxloglist'][u] else '导入成功')
    #                                 )
    #                             self.add_err_log(log_str)
    #                     except Exception as e:
    #                         print(e)
    #                     # 插入日志 end -----------------------------------------------------
    #
    #                     print('请求成功，返回正确')
    #                     # self.add_err_log('资料进度：{0}；请求成功，返回0，报错信息：{1}'.format(i, str(field_dict['cxajaxloglist'] if 'cxajaxloglist' in field_dict else '')))
    #                     # print('请求成功，返回正确')
    #                     # print('data_dict_encoding: {0}'.format(data_dict_encoding))
    #                     # print('data_dict: {0}'.format(json.dumps(data_dict_list, ensure_ascii=False)))
    #                     # print(111111111111111111111111111111111111111111)
    #                     success_num = field_dict['returnvalue']["success"]
    #                     fail_num = field_dict['returnvalue']["error"]
    #                     # cal_signal = pyqtSignal(np.ndarray, np.ndarray)
    #
    #                     # self.download_proess_signal.emit(int(proess))
    #
    #                     # 发射进度条更新信号
    #                     self.sig.dataChanged.emit(success_num, fail_num, i + 1)
    #
    #                     # self.table1.parent().progress_box.update_progress(success_num, fail_num)
    #
    #                     # print('成功{0}条'.format(success_num))
    #                     # print('失败{0}条'.format(fail_num))
    #                     # return_dict = {'state': -1, 'success_num': success_num, 'fail_num': fail_num}
    #                     # return return_dict
    #
    #                 # 一百天传完后，清空队列，再传下一百条
    #                 data_dict_list = []
    #
    #         # 发射导入结束信号
    #         self.sig.dataFinished.emit(True)
    #
    #     # _thread.start_new_thread(start_run, ())
    #
    #     self.import_thread = MyThreadFunc(start_run)
    #     self.import_thread.start()
    #     # 关闭按钮点击，终止线程
    #     table_parent_widget.progress_box.close_btn.clicked.connect(self.import_thread.stop)
    #     table_parent_widget.progress_box.cancel_btn.clicked.connect(table_parent_widget.progress_box.close_btn.click)
    # # 导入资料方法 end -------------------------------------------------------------------------


